<template>
  <el-card>
    <div>
      <el-button
        type="primary"
        icon="el-icon-plus"
        size="default"
        @click="showAdd"
        >添加</el-button
      >
      <el-table border style="margin: 20px 0" :data="trademarks.records">
        <el-table-column label="序号" type="index" width="80" align="center" />
        <el-table-column label="品牌名称" prop="tmName" />
        <el-table-column label="品牌LOGO">
          <template v-slot:default="scope">
            <img :src="scope.row.logoUrl" style="width: 100px; height: 60px" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row, $index }">
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-edit"
              @click="showUpdate(row)"
              >修改</el-button
            >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="remove(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        style="text-align: center"
        @size-change="handleSizeChange"
        @current-change="getTrademarks"
        :current-page="page"
        :page-sizes="[3, 6, 9]"
        :page-size="limit"
        :total="trademarks.total"
        layout="prev, pager, next, jumper,->, sizes, total"
      >
      </el-pagination>
    </div>
    <!-- dialog 弹框 -->
    <el-dialog :title='this.form.id? "修改品牌":"添加品牌"' :visible.sync="isShowDialog">
      <el-form
        :model="form"
        :rules="rules"
        ref="ruleForm"
        style="width: 80%"
        label-width="100px"
      >
        <el-form-item label="品牌名称" prop="tmName">
          <el-input v-model="form.tmName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" prop="logoUrl">
          <!-- 上传图片框 -->
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
          <img v-if="form.logoUrl" :src="form.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__tip" slot="tip">
              只能上传jpg/png文件，且不超过50kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShowDialog = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  name: "Trademark",
  data() {
    return {
      trademarks: [], // 当前页的品牌列表
      total: 0, // 总数量
      page: 1, // 当前页码
      limit: 3, // 每页数量
      isShowDialog: false, // 是否显示添加的对话框
      form: {
        // 用来收集品牌添加的数据
        tmName: "", // 品牌名称
        logoUrl: "", // 品牌logo的url
      },

      imageUrl: "", //上传图片的容器
      //表单验证
      rules: {
        tmName: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "品牌名称长度在 2 到 10 个字符",
            trigger: "change",
          },
        ],
        logoUrl: [
          {
            required: true,
            message: "请上传正确的品牌LOGO",
          },
        ],
      },
    };
  },

  //挂载钩子
  mounted() {
    this.getTrademarks();
  },

  //方法
  methods: {
    //获取数据
    async getTrademarks(page = 1) {
      this.page = page;
      // 调用接口请求函数发获取列表数据的请求
      let result = await this.$API.TradeApi.getList(this.page, this.limit);
      this.trademarks = result.data;
      console.log(result);
    },

    //每页显示数量
    handleSizeChange(pageSize) {
      this.limit = pageSize;
      // 重新获取第1页显示
      this.getTrademarks(1); // 也可不传1
    },
    //点击添加按钮
    showAdd() {
      this.isShowDialog = true;
      //每次关闭清空数据
      this.form = {
        tmName: "",
        logoUrl: "",
      };
      this.$nextTick(() => {
        // nextTick回调函数是迟延到界面更新后执行的   面试说
        this.$refs.ruleForm.resetFields();
      });
    },
    //点击修改按钮
    showUpdate(row) {
      this.isShowDialog = true;
      //根据ID展示
      this.form = row;
    },
    //点击dialog确定按钮
    dialogFormVisible() {
      //进行表单验证
      this.$refs.ruleForm.validate(async (success) => {
        //如果成功  就发送请求
        if (success) {
          this.isShowDialog = false;
          //发请求
          let result = await this.$API.TradeApi.dialogFormVisible(this.form);
          if (result.code === 200) {
            this.$message({
              type: "success",
              message: this.form.id ? "修改品牌成功" : "添加品牌成功",
            });
            //修改成功后再次获取最新的列表数据
            this.getTrademarks();
          }
        }
      });
    },
    //上传图片
    handleAvatarSuccess(res, file) {
      this.form.logoUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 删除按钮
    remove(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let result = await this.$API.TradeApi.remove(row.id);
          if (result.code === 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //重新加载商品页面
            this.getTrademarks(this.page);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>